package org.abratuhi.kvngroupware.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.abratuhi.kvngroupware.client.KVNGroupwareClient;
import org.abratuhi.kvngroupware.gui.GUIJokeManager;
import org.abratuhi.kvngroupware.model.Joke;
import org.abratuhi.kvngroupware.network.Message;
import org.jdom.Element;

public class JokeManagerController implements ActionListener, WindowListener, ListSelectionListener{
	
	KVNGroupwareClient kvnclient;
	GUIJokeManager gui;
	ArrayList<Joke> jokes;
	
	public JokeManagerController(KVNGroupwareClient kvnclient, GUIJokeManager gui, ArrayList<Joke> jokes){
		this.kvnclient = kvnclient;
		this.gui = gui;
		this.jokes = jokes;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		if(cmd.equals(GUIJokeManager.AC_ADD_JOKE)){
			Joke newjoke = new Joke(gui.getJokeText(), kvnclient.getUser().getId(), kvnclient.getTeam().getId());
			
			String[] attrNames = new String[]{"type", "action"};
			String[] attrValues= new String[]{"application", "create_joke"};
			String text = new String();
			Element[] content = new Element[]{newjoke.toElement()};
			
			Message message = kvnclient.createMessage(attrNames, attrValues, text, content);
			kvnclient.sendMessage(message);
		}
		else if(cmd.equals(GUIJokeManager.AC_UPDATE_JOKE)){
			if(gui.selectedJoke != -1){
				jokes.get(gui.selectedJoke).setText(gui.getJokeText());
				
				String[] attrNames = new String[]{"type", "action"};
				String[] attrValues= new String[]{"application", "update_joke"};
				String text = new String();
				Element[] content = new Element[]{jokes.get(gui.selectedJoke).toElement()};
				
				Message message = kvnclient.createMessage(attrNames, attrValues, text, content);
				kvnclient.sendMessage(message);
			}
		}
		else if(cmd.equals(GUIJokeManager.AC_RATE_JOKE)){
			if(gui.selectedJoke != -1){
				jokes.get(gui.selectedJoke).setRating(gui.getJokeRating());

				String[] attrNames = new String[]{"type", "action"};
				String[] attrValues= new String[]{"application", "update_joke"};
				String text = new String();
				Element[] content = new Element[]{jokes.get(gui.selectedJoke).toElement()};

				Message message = kvnclient.createMessage(attrNames, attrValues, text, content);
				kvnclient.sendMessage(message);
			}
		}
	}

	@Override
	public void windowActivated(WindowEvent e) {}

	@Override
	public void windowClosed(WindowEvent e) {}

	@Override
	public void windowClosing(WindowEvent e) {
		gui.disposeJFrame();
	}

	@Override
	public void windowDeactivated(WindowEvent e) {}

	@Override
	public void windowDeiconified(WindowEvent e) {}

	@Override
	public void windowIconified(WindowEvent e) {}

	@Override
	public void windowOpened(WindowEvent e) {}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		gui.selectedJoke = gui.tableJokes.getSelectedRow();
		if(gui.selectedJoke != -1) gui.setJokeText(jokes.get(gui.selectedJoke).getText());
	}

}
